import * as Router from 'koa-router';
import { taskCenter, downFile, dbDownFile } from '../tools';
import { downOneList, downOneGropImgs } from '../downCenter';
import { DB } from '../db/modules';

let router = new Router();

router.all('/task', (ctx, next) => {
  ctx.body.data = taskCenter.getTasksNumber();
});
router.all('/down', async (ctx, next) => {
  DB.Resource.update(
    {
      loading: true
    },
    {
      loading: false
    }
  );
  async function down() {
    if (!taskCenter.getTasksNumber().willListLength) {
      let list = await DB.Resource.find({
        is_down: false,
        loading: false
      })
        .limit(100)
        .sort({ fail_number: 1 });

      list.map(item => {
        dbDownFile({
          url: item.target_url,
          localPath: item.url,
          parentId: item.parent_id,
          index: item.index
        });
      });
      if (list.length) {
        setTimeout(down, 1000);
      }
    } else {
      setTimeout(down, 1000);
    }
  }
  down();
  let list = await DB.Resource.find({
    is_down: false,
    loading: false
  })
    .limit(1)
    .sort({ fail_number: 1 });
  ctx.body.data = list;
});
router.all('/test', async (ctx, next) => {
  ctx.body.data = {
    data: await DB.Resource.find({
      is_down: false,
      loading: false
    })
      .limit(100)
      .sort({ fail_number: 1 }),
    count: 1112132312
  };
  ctx.body.msg = '成功';
  ctx.body.code = 200;
});
router.all('/downList', async (ctx, next) => {
  let i = 2;
  function down() {
    if (i > 1327) return;
    if (
      taskCenter.getTasksNumber().willListLength ||
      taskCenter.getTasksNumber().runningListLength
    ) {
      setTimeout(down, 1000);
    } else {
      downOneList('http://153na.com/tupianqu/KSE/index_' + i + '.html');
      i++;
      setTimeout(down, 1000);
    }
  }
  down();
  // let end = 1327;
  // downOneList('http://153na.com/tupianqu/KSE/');
  // for (let i = end; i > 2; i--) {
  //   setTimeout(
  //     i => {
  //       downOneList('http://153na.com/tupianqu/KSE/index_' + i + '.html');
  //     },

  //     0,
  //     i
  //   );
  // }

  // downOneGropImgs('http://153na.com/tttppp/1103227.html');
  ctx.body.data = ctx;
});
router.all('/resource', async (ctx, next) => {
  ctx.body = await DB.Resource.find();
});
router.all('/resourceImg', async (ctx, next) => {
  let list = await DB.Resource.find();
  ctx.body = '<h1>总共 ' + list.length + '张 <h1>';

  ctx.body +=
    list
      .splice(list.length - 100)
      .map(item => {
        return `<img src="${item.url}" >`;
      })
      .join('') +
    '<style>*{margin:0;padding:0;}img{width:700px;margin:auto;display:block;}</style>';
});

// router.all('/r', async (ctx, next) => {
//   let list = await DB.Resource.remove({});
//   await DB.ImageGroup.remove({});

//   ctx.body.data = list;
// });
router.all('/imagegroupList', async (ctx, next) => {
  let data = await DB.ImageGroup.find();
  ctx.body.data = data
    .map(item => {
      return item.name && item;
    })
    .filter(Boolean);
});
export default router;

// for (let i = 1; i <= 2000; i++) {
//   setTimeout(
//     i => {
//       downOneList('http://153na.com/tupianqu/KSE/index_' + i + '.html');
//     },

//     0,
//     i
//   );
// }
